package product;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class MemberHolyselect extends JInternalFrame {
	
	Connection conn;
	ProductMain pm;
	PreparedStatement pstmt;
	ResultSet rs;
	ResultSetMetaData rsmd;
	DefaultTableModel model;
	Vector<String> v;
	
	private JScrollPane scrollPane;
	private JTable table;

	public MemberHolyselect(Connection conn, ProductMain pm) {
		super("휴가조회", true, true, true, false);
		this.conn=conn;
		this.pm=pm;
		setBounds(100, 100, 450, 300);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);

	}
	
	public void columnadd() {
		if (table!=null) table=null;

		try {    

			String sql = "select * from holydata";
			pstmt = conn.prepareStatement(sql);


			rs = pstmt.executeQuery();// 리절트셋에 대한 부가정보를 갖는 객체얻어오기
			rsmd = rs.getMetaData();// 컬럼명 테이블에 셋팅

			int colCount = rsmd.getColumnCount();
			String t[] = new String[colCount + 1];
			t[0] = "NO";

			for (int i = 1; i <= colCount; i++) {
				String colName = rsmd.getColumnName(i);
				t[i] = colName;
			}

			for(int i=0; i<=t.length-1; i++){
				System.out.println(t[i]);
			}

			model = new DefaultTableModel(t,0);
			scrollPane.setViewportView(getTable());

			int k=0; 
			while (rs.next()) {
				v= new Vector<String>();
				k=k+1;
				v.add(""+k);
				for (int j = 1; j <= colCount; j++) {
					v.add(rs.getString(j));
				}
				model.addRow(v);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	
	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					
					
				}
			});
		}
		return table;
	}
}
